
angular.module("driver").controller("RegisterCtrl", ['$scope', '$meteor', '$rootScope', '$state', '$ionicPopup', 'Map', 'Translate',
  function($scope, $meteor, $rootScope, $state, $ionicPopup, Map, Translate){
  		$scope.info = {
  		};


    $scope.register = function() {
      if (!$scope.info.username || !$scope.info.password || !$scope.info.realname) {
        $scope.showAlert('注册失败', '每一项都需要填写!');
        return;
      }
      if ($scope.info.confixpassword !== $scope.info.password) {
        $scope.showAlert('注册失败', '密码不一致!');
        return;
      }
      var option = {
        username: $scope.info.username,
        password: $scope.info.password,
        email: $scope.info.email || ($scope.info.username + '@yeah.net'),
        profile: {
          //name: $scope.info.nickname,

        },
        others: {
          realname: $scope.info.realname,
          tel: $scope.info.username
        },
      };
      
   
        //option.roles = $scope.info.isdriver ? ['driver'] : ['customer'];
        //option.roles = option.roles.concat( ['admin'] );
        //console.log(option)
  			Accounts.createUser(option, function (error) {
  				if (error) {
  					$scope.showAlert('注册失败', Translate.translate(error.reason));
          }
          else {
            //Map.setLocation(pos.position);
            $meteor.call('updateLocation', Map.getLocation());
            //Map.geolocation.getCurrentPosition();
            //console.log( Map.geolocation);
            if ($scope.info.isdriver)
              $state.go('driverinfo');
            else
              $state.go('account');
          }
  			});
  		};
  		// An alert dialog
  		$scope.showAlert = function(title, temp) {
  			var alertPopup = $ionicPopup.alert({
  				title: title,
  				template: temp,

        okType: 'button-royal',
        okText: '确定'
      });
      alertPopup.then(function(res) {
        //console.log('Thank you for not eating my delicious ice cream cone');
      });
    };

  }
]);
